package com.uha.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uha.reggie.common.ReggieResult;
import com.uha.reggie.entity.Employee;
import com.uha.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;


/**
 * @author uha
 * @date 2023/6/15
 * @apiNote
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录  MD5加密 查看员工状态 存入Session
     * @param employee emp
     * @param request request
     * @return ReggieResult<T>
     */
    @PostMapping("/login")
    public ReggieResult<Employee> login(@RequestBody Employee employee, HttpServletRequest request){

    String password = employee.getPassword();

    password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        /**
         * username字段是唯一约束字段 即用户名不可重复 就可以调用getOne()
         */
        Employee emp = employeeService.getOne(wrapper);

        if (emp == null){
            return  ReggieResult.error("用户不存在，请注册~");
        }

        if (!emp.getPassword().equals(password)){
            return  ReggieResult.error("密码错误，请重新输入~");
        }

        if (emp.getStatus() == 0){
            return  ReggieResult.error("该账号禁用啦~");
        }

        request.getSession().setAttribute("employee",emp.getId());

        return ReggieResult.success(emp);
    }

    /**
     * 员工登出
     * @param request request
     * @return  ReggieResult<String>
     */
    @PostMapping("/logout")
    public ReggieResult<String> loginout(HttpServletRequest request){

        request.getSession().removeAttribute("employee");

        return ReggieResult.success("退出成功~");
    }

    /**
     * 新增员工
     * @param employee emp
     * @return ReggieResult<String>
     */
    @PostMapping
    public ReggieResult<String> addEmp(@RequestBody  Employee employee){

        log.info("新增员工信息： {}" + employee.toString());

        // 设置初始密码  并MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

         // 下面这部分MP字段上加有注解 自动填充  不使用 try-catch  进行全局异常捕获

        employeeService.save(employee);

        return ReggieResult.success("员工新增成功~");
    }

    /**
     * MP 分页
     * @param page page
     * @param pageSize pageSize
     * @param name name
     * @return
     */
    @GetMapping("/page")
    public ReggieResult<Page> page(int page,int pageSize,String name){
        log.info("page = {},pageSize = {},name = {}",page,pageSize,name);

        //构造分页构造器
        Page<Employee> pageInfo = new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(name),Employee::getName,name);

        employeeService.page(pageInfo,queryWrapper);

        return ReggieResult.success(pageInfo);

    }

    /**
     * 通用修改方法
     * Long  js 16位之后丢失精度 转String
     * @param employee emp
     */
    @PutMapping
    public ReggieResult<String> UpdateEmp(@RequestBody  Employee employee){


        employeeService.updateById(employee);

        return ReggieResult.success("员工信息修改成功~");
    }

        /**
        * 数据回显
         */
        @GetMapping("/{id}")
        public ReggieResult<Employee> hx(@PathVariable("id") Long id ){


            Employee emp = employeeService.getById(id);
            if(emp != null){
                return ReggieResult.success(emp);
            }

            return ReggieResult.error("错误~");
        }



}
